package com.ruoyi.web.core.utils;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * <pre>
 *    description
 * </pre>
 *
 * @author zhangli
 * @version 1.0
 * @since 2025/3/31
 */
@Slf4j
@Component
public class OssUtil {

    private final String bucketName = "h5-face";

    private OSS ossClient;

    public OssUtil() {
        // 设置 OSS Endpoint 和 Bucket 名称
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

        // 替换为您的 Bucket 区域
        String region = "cn-hangzhou";

        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeout(30000);
        clientConfiguration.setSocketTimeout(30000);
        clientConfiguration.setRequestTimeout(30000);

        // 从环境变量中获取访问凭证。运行本代码示例之前，请先配置环境变量
        DefaultCredentialProvider credentialsProvider = new DefaultCredentialProvider("LTAI5tCrsnnm9hqoWWtHGdCC", "n4Ewzrb27qs8wdYqbvKpvA54NWAYg3");

        // 创建 OSSClient 实例
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // 显式声明使用 V4 签名算法
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .region(region)
                .clientConfiguration(clientConfiguration)
                .build();


    }


    public String upload(String url) {

        try {

            SimpleDateFormat sdf = new SimpleDateFormat("MM_dd_HH");
            String format = sdf.format(new Date());

            String name = format+ "_" + RandomStringUtils.randomAlphanumeric(12) + ".jpg";

            String objectName = "merge/" + name;

            InputStream inputStream = new URL(url).openStream();
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            log.info("oss upload result:{}", result.getResponse() );
            return objectName;
        } catch (Exception oe) {
            log.error(oe.getMessage(),oe);
        }

        return "";
    }





}
